package com.changpay.zywy.wight

import android.app.Activity
import android.content.Context
import android.text.InputType
import android.text.method.PasswordTransformationMethod
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout

import com.changpay.zywy.ext.loge
import kotlinx.android.synthetic.main.layout_input_item.view.*
import android.os.CountDownTimer
import android.support.v4.content.ContextCompat
import android.text.InputFilter
import com.changpay.zywy.R


class InputItemView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    private var canEdit: Boolean
    /** 左边控件显示的文本*/
    var textValue: String? = ""  // ?表示任何时候都不会报空指针异常


    /** 输入控件提示显示的文本*/
    var hintValue: String? = ""  // ?表示任何时候都不会报空指针异常


    /** 输入控件类型*/
    var inputType: String? = ""

    /** 输入框的长度*/
    var maxLine: Int = 0


    var leftImage: Int = 0


    /** 事件是否是点击事件*/
    var isShowMoreCode: Boolean = false

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.InputItemView)
        textValue = typedArray.getString(R.styleable.InputItemView_itemName)
        hintValue = typedArray.getString(R.styleable.InputItemView_hintText)
        inputType = typedArray.getString(R.styleable.InputItemView_inputType)
        maxLine = typedArray.getInt(R.styleable.InputItemView_maxLines, maxLine)
        leftImage = typedArray.getResourceId(R.styleable.InputItemView_leftImage, R.mipmap.login_phone)
        isShowMoreCode = typedArray.getBoolean(R.styleable.InputItemView_isShowMoreCode, isShowMoreCode)
        canEdit = typedArray.getBoolean(R.styleable.InputItemView_canEdit, true)
        initView()
        typedArray.recycle()
    }

    private fun initView() {
        View.inflate(context, R.layout.layout_input_item, this)
        tv_get_code.visibility = if (isShowMoreCode) View.VISIBLE else View.GONE
        textValue?.let {
            tv_item_name.text = it
        }
        et_input.hint = hintValue
        if (maxLine != 0) et_input.filters = arrayOf(InputFilter.LengthFilter(maxLine))
        loge(inputType)
        when (inputType) {
            "text" -> et_input.inputType = InputType.TYPE_CLASS_TEXT
            "password" -> {
                et_input.transformationMethod = PasswordTransformationMethod.getInstance()
                et_input.inputType = InputType.TYPE_CLASS_TEXT
            }
            "number" -> et_input.inputType = InputType.TYPE_CLASS_NUMBER
            else -> et_input.inputType = InputType.TYPE_CLASS_TEXT
        }
        val dra =  ContextCompat.getDrawable(context,leftImage)
        et_input.setCompoundDrawablesWithIntrinsicBounds(dra,null,null, null)
        et_input.isEnabled = canEdit
    }


    fun starCode() {
        val mTimer = tv_get_code
        val test = context as Activity
        object : CountDownTimer(60000, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                if (test.isFinishing) {
                    this.cancel()
                    return
                }
                mTimer.isEnabled = false
                mTimer.text = (millisUntilFinished / 1000).toString() + "s"
            }

            override fun onFinish() {
                if (test.isFinishing) {
                    this.cancel()
                    return
                }
                mTimer.isEnabled = true
                mTimer.text = "重新获取"
            }
        }.start()
    }

    fun getInputText(): String {
        return et_input.text.toString()
    }


    fun setInputText(text: String) {
        et_input.setText(text)
    }


    fun setCodeOnclickListe(onClickListener: OnClickListener) {
        tv_get_code.setOnClickListener(onClickListener)
    }
}

